package com.wnxy.demo.service.impl;

import com.wnxy.demo.service.UserService;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    @Override
    public String test() {
        return "正常执行的方法";
    }

    @Override
    public String findById() {
        boolean flag = true;
        while (flag) {
            //FIXME 模拟
        }
        return "CPU占用过高问题模拟";
    }

    @Override
    public String findByName() {
        Object o1 = new Object();
        Object o2 = new Object();
        // 创建线程1
        new Thread(()->{
            synchronized (o1){
                try {
                    Thread.sleep(1000);
                    synchronized (o2){
                        System.out.println("线程1执行");
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        // 创建线程2
        new Thread(()->{
            synchronized (o2){
                try {
                    Thread.sleep(1000);
                    synchronized (o1){
                        System.out.println("线程2执行");
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        return "死锁问题模拟分析模拟";
    }
}
